package com.jike.noobmoney.mvp.view.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.NumEntity;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.AppUtils;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import org.w3c.dom.Text;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2019/3/28
 * 每日挑战
 */

public class ChallengeActivity extends BaseActivity implements IView {

//    @BindView(R.id.tv_money)
//    TextView tvMoney;
//
//    @BindView(R.id.tv_new_yaoqing)
//    TextView tvNewYaoqing;
//    @BindView(R.id.tv_new_shipin)
//    TextView tvNewShipin;
//    @BindView(R.id.tv_new_gongzhonghao)
//    TextView tvNewGongzhonghao;
//    @BindView(R.id.tv_new_renwu)
//    TextView tvNewRenwu;
     @BindView(R.id.tv_title)
     TextView tv_Title;
//    @BindView(R.id.tv_newtask3)
//    TextView tvNewRenwu3;
//    @BindView(R.id.tv_newtask6)
//    TextView tvNewRenwu6;
//
//    @BindView(R.id.tv_new_jinqun)
//    TextView tvNewJinqun;
//    @BindView(R.id.tv_invite_num)
//    TextView tvInviteNum;

    @BindView(R.id.ll_task)
    LinearLayout ll_task;

    @BindView(R.id.ll_game)
    LinearLayout ll_game;

    @BindView(R.id.tv_o)
    TextView tv_o;
    @BindView(R.id.tv_w1)
    TextView tv_w1;
    @BindView(R.id.tv_sum1)
    TextView tv_sum1;

    @BindView(R.id.tv_m2)
    TextView tv_m2;
    @BindView(R.id.tv_w2)
    TextView tv_w2;
    @BindView(R.id.tv_sum2)
    TextView tv_sum2;

    @BindView(R.id.tv_l3)
    TextView tv_l3;
    @BindView(R.id.tv_w3)
    TextView tv_w3;
    @BindView(R.id.tv_sum3)
    TextView tv_sum3;
    @BindView(R.id.tv_m4)
    TextView tv_m4;

    @BindView(R.id.tv_w4)
    TextView tv_w4;
    @BindView(R.id.tv_sum4)
    TextView tv_sum4;

    @BindView(R.id.tv_r)
    TextView tv_r;
    @BindView(R.id.tv_rr)
    TextView tv_rr;
    @BindView(R.id.tv_rc)
    TextView tv_rc;
    @BindView(R.id.tv_rd)
    TextView tv_rd;


    private UserPresenter userPresenter;
    private String userid;

    private int invite = -1;
    private int video = -1;
    private int attention = -1;
    private int complete = -1;
    private int team = -1;
    private int complete3;
    private int complete6;



    @Override
    protected int layoutResID() {
        return R.layout.activity_challenge_layout;
    }

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        tv_Title.setText("每日挑战");
        refreshData();
//        newTaskList();
    }


    /**
     * 获取完成情况
     */
    private void refreshData() {
        showProgress();
        userPresenter.getNum(userid, ConstantValue.RequestKey.getNum);
    }

    /**
     * 获取新手任务列表
     */
    private void newTaskList() {
        showProgress();
        userPresenter.newusertasklist(userid, ConstantValue.RequestKey.new_task_list);
    }

    @Override
    public boolean ismain() {
        return true;
    }
    @Override
    public Boolean isfits() {
        return true;
    }

    @OnClick({R.id.ll_task,R.id.iv_back,R.id.ll_game
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_task:
                startActivity(new Intent(context,TastListActivity.class));
                break;
            case R.id.ll_game:
                startActivity(new Intent(context,GameCenterActivity.class));
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    /**
     * 点击分享
     */
    public void recommend() {
        String recode = SPUtils.getInstance().getString(ConstantValue.SpType.recode);//签到页
        String taskurl = SPUtils.getInstance().getString(ConstantValue.SpType.taskurl);
        String url = !TextUtils.isEmpty(taskurl) ? taskurl + "?recode=" + recode : ConstantValue.COMMENTURLCOPY + recode;
        UMWeb web = new UMWeb(url);
        web.setTitle("小白赚钱");
        web.setThumb(new UMImage(context, R.mipmap.ic_launcher));
        web.setDescription(getString(R.string.text_yaoqing1));
        ShareAction shareAction = new ShareAction(this);
        shareAction.withMedia(web);
        shareAction.setDisplayList(SHARE_MEDIA.QZONE, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        shareAction.open();
    }


    private void lingqu(String flag) {
        if (AppUtils.isFastClick()) {
            ToastUtils.showShortToastSafe("操作有点频繁,稍后重试");
            return;
        }
        showProgress();
        userPresenter.getreceive(userid, flag, ConstantValue.RequestKey.new_task_receive);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
//        if (ConstantValue.RequestKey.getMoneyView.equals(actionType)) {
//            //获取用户余额
//            MoneyEntity.UserBean userBean = (MoneyEntity.UserBean) data;
//            tvMoney.setText("" + userBean.getMoney());
//        }
        if (ConstantValue.RequestKey.getNum.equals(actionType)) {
            NumEntity bean=(NumEntity) data;
            //获取任务完成情况
            tv_o.setText(bean.omoney+"");
            tv_w1.setText(""+bean.onumber);
            tv_sum1.setText(""+bean.osumnumber);


            tv_m2.setText(""+bean.o2money);
            tv_w2.setText(""+bean.o2number);
            tv_sum2.setText(""+bean.o2sumnumber);

            tv_l3.setText(""+bean.pmoney);

            tv_w3.setText(""+bean.pnumber);
            tv_sum3.setText(""+bean.psumnumber);

            tv_m4.setText(""+bean.p2money);
            tv_w4.setText(""+bean.p2number);
            tv_sum4.setText(""+bean.p2sumnumber);

            refreshNewView(tv_r,bean.offernumber);
            refreshNewView(tv_rr,bean.offernumber2);
            refreshNewView(tv_rc,bean.playforfunnumber);
            refreshNewView(tv_rd,bean.playforfunnumber2);

        }

        //0是未完成  1是完成  2是已领取
//        if (ConstantValue.RequestKey.new_task_list.equals(actionType)) {
//            NewTaskListEntity newTaskListEntity = (NewTaskListEntity) data;
//            invite = newTaskListEntity.getInvite();
//            video = newTaskListEntity.getVideo();
//            attention = newTaskListEntity.getAttention();
//            complete = newTaskListEntity.getComplete();
//            team = newTaskListEntity.getTeam();
//            complete3 = newTaskListEntity.getComplete2();
//            complete6 = newTaskListEntity.getComplete3();
//
//            refreshNewView(tvNewYaoqing, invite);
//            refreshNewView(tvNewShipin, video);
//            refreshNewView(tvNewGongzhonghao, attention);
//            refreshNewView(tvNewRenwu, complete);
//            refreshNewView(tvNewJinqun, team);
//            refreshNewView(tvNewRenwu3, complete3);
//            refreshNewView(tvNewRenwu6, complete6);
//        }
//
        if (ConstantValue.RequestKey.new_task_receive.equals(actionType)) {
            ToastUtils.showShortToastSafe(msg);
            //领取成功
            refreshData();
        }

    }

    private void refreshNewView(TextView textView, int status) {
        switch (status) {
            case 0:
                //未完成
                textView.setText("去完成");
                switch (textView.getId())
                {
                    case R.id.tv_r:
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                startActivity(new Intent(context,TastListActivity.class));
                            }
                        });
                        break;
                    case R.id.tv_rr:
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                startActivity(new Intent(context,TastListActivity.class));
                            }
                        });
                        break;
                    case R.id.tv_rc:
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                startActivity(new Intent(context,GameCenterActivity.class));
                            }
                        });
                        break;
                    case R.id.tv_rd:
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                startActivity(new Intent(context,GameCenterActivity.class));
                            }
                        });
                        break;

                }
                break;
            case 1:
                //完成
                textView.setText("领取奖励");
                switch (textView.getId())
                {
                    case R.id.tv_r:
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                lingqu("1");
                            }
                        });
                        break;
                    case R.id.tv_rr:
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                lingqu("2");
                            }
                        });
                        break;
                    case R.id.tv_rc:
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                lingqu("3");
                            }
                        });
                        break;
                    case R.id.tv_rd:
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                lingqu("4");
                            }
                        });
                        break;

                }
                break;
            case 2:
                //已领取
                textView.setText("已完成");
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
                break;

        }
    }



    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 201 && resultCode == RESULT_OK) {
            newTaskList();
        }
    }
}
